package dat02;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test08 {
    /**
     * 要求用户输入若干员工信息，格式为：
     * name,age,gender,salary;name,age,gender,salary;....
     * 例如:
     * 张三,25,男,5000;李四,26,女,6000;...
     * 然后将每个员工信息解析成Person对象。并存入到一个数组中。
     * 然后循环数组，输出每一个员工信息(输出使用toString返回的字符串)
     * @author Admin
     *
     */
    public static void main(String[] args) {

        add();

    }
    public static void add() {
        try (Scanner sc = new Scanner(System.in)) {
            List<Person> list = new ArrayList<>();
            boolean exit = false;

            do {
                System.out.println("请输入若干员工信息（输入'exit'退出）");
                String s = sc.nextLine();

                if (s.equalsIgnoreCase("exit")) {
                    exit = true;
                    continue;
                }

                String[] records = s.split(";");
                for (String record : records) {
                    if (record.trim().isEmpty()) {
                        System.out.println("输入为空，请重新输入");
                        add();
                        break;
                    }
                    String[] split = record.split(",");
                    if (split.length != 4) {
                        System.out.println("输入格式有误，请重新输入");
                        break;
                    }
                    try {
                        String name = split[0].trim();
                        int age = Integer.parseInt(split[1].trim());
                        String gender = split[2].trim();
                        int salary = Integer.parseInt(split[3].trim());

                        if (age <= 0 || salary < 0 || (!gender.equals("男") && !gender.equals("女"))) {
                            System.out.println("输入数据无效，请重新输入");
                            break;
                        }

                        list.add(new Person(name, age, gender, salary));
                    } catch (NumberFormatException e) {
                        System.out.println("输入的数据格式不正确，请重新输入");
                        break;

                    }
                }
            } while (!exit);

            for (Person p : list) {
                System.out.println(p);
            }
        }
    }
}
